<?php

function translateDateTime($date, $type)
{
	$date_timestamp = strtotime($date);
	
	$month_name = array(1 => "january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december");
	$day_name = array(1 => "monday", 2 => "tuesday", 3 => "wednesday", 4 => "thursday", 5 => "friday", 6 => "saturday", 7 => "sunday");
	
	if($type == DATE_TRANSLATE_NORMAL)
	{				
		return sprintf("%d %s %d", date("j", $date_timestamp), $month_name[date('n', $date_timestamp)], date('Y', $date_timestamp));
	}
	else if($type == DATE_TRANSLATE_SIMPLE_WORD)
	{
		$cdate = mktime(0, 0, 0, date("n", $date_timestamp),date("j", $date_timestamp),date("Y", $date_timestamp));   
		$today = mktime(0, 0, 0, date("n"),date("j"),date("Y"));        
		$day_difference =  floor(($today - $cdate)/60/60/24); 
				
		if($day_difference > 0)
		{
			if($day_difference >= 14)
			{							
				return date("j", $date_timestamp) . " " . $month_name[date('n', $date_timestamp)] . " " . date('Y', $date_timestamp);										
			}
			else
			{
				if($day_difference == 1)
				{
					return "yesterday at " . date("G:i", $date_timestamp);
				}
				else if($day_difference < 6)
				{
					
					return $day_name[date("N", $date_timestamp)] . ", " . date("G:i", $date_timestamp);			
				}
				else
				{
					return $day_difference." ". "days ago";
				}
			}
		}
		else
		{
			$dateDiff    = time() - $date_timestamp;
			$fullDays    = floor($dateDiff/(60*60*24));
			$fullHours   = floor(($dateDiff-($fullDays*60*60*24))/(60*60));
			$fullMinutes = floor((($dateDiff-($fullDays*60*60*24))-($fullHours*60*60)) / 60);
		
			if($fullHours > 0)
			{
				if($fullHours == 1)
				{
					return "about one hour ago";
				}
				else
				{
					return $fullHours." "."hours ago";
				}
			}
			else if($fullMinutes > 0)
			{
				if($fullMinutes == 1)
				{
					return "a minute ago";			
				}
				else
				{
					return $fullMinutes." ". "minutes ago";
				}
			}
			else if($fullMinutes == 0)
			{
				return "at the moment";
			}	
		}
			
	}	
}

function translate_url_looklike($string)
{
	$result = strtolower($string);
	return str_replace(array(" "), "-", $result);
}

function htmlEscapeAndLinkUrls($text)
{
	$rexProtocol = '(https?://)?';
	$rexDomain   = '((?:[-a-zA-Z0-9]{1,63}\.)+[-a-zA-Z0-9]{2,63}|(?:[0-9]{1,3}\.){3}[0-9]{1,3})';
	$rexPort     = '(:[0-9]{1,5})?';
	$rexPath     = '(/[!$-/0-9:;=@_\':;!a-zA-Z\x7f-\xff]*?)?';
	$rexQuery    = '(\?[!$-/0-9:;=@_\':;!a-zA-Z\x7f-\xff]+?)?';
	$rexFragment = '(#[!$-/0-9:;=@_\':;!a-zA-Z\x7f-\xff]+?)?';
	$rexUrlLinker = "{\\b$rexProtocol$rexDomain$rexPort$rexPath$rexQuery$rexFragment(?=[?.!,;:\"]?(\s|$))}";
	
	$validTlds = array_fill_keys(explode(" ", ".ac .ad .ae .aero .af .ag .ai .al .am .an .ao .aq .ar .arpa .as .asia .at .au .aw .ax .az .ba .bb .bd .be .bf .bg .bh .bi .biz .bj .bm .bn .bo .br .bs .bt .bv .bw .by .bz .ca .cat .cc .cd .cf .cg .ch .ci .ck .cl .cm .cn .co .com .coop .cr .cu .cv .cx .cy .cz .de .dj .dk .dm .do .dz .ec .edu .ee .eg .er .es .et .eu .fi .fj .fk .fm .fo .fr .ga .gb .gd .ge .gf .gg .gh .gi .gl .gm .gn .gov .gp .gq .gr .gs .gt .gu .gw .gy .hk .hm .hn .hr .ht .hu .id .ie .il .im .in .info .int .io .iq .ir .is .it .je .jm .jo .jobs .jp .ke .kg .kh .ki .km .kn .kp .kr .kw .ky .kz .la .lb .lc .li .lk .lr .ls .lt .lu .lv .ly .ma .mc .md .me .mg .mh .mil .mk .ml .mm .mn .mo .mobi .mp .mq .mr .ms .mt .mu .museum .mv .mw .mx .my .mz .na .name .nc .ne .net .nf .ng .ni .nl .no .np .nr .nu .nz .om .org .pa .pe .pf .pg .ph .pk .pl .pm .pn .pr .pro .ps .pt .pw .py .qa .re .ro .rs .ru .rw .sa .sb .sc .sd .se .sg .sh .si .sj .sk .sl .sm .sn .so .sr .st .su .sv .sy .sz .tc .td .tel .tf .tg .th .tj .tk .tl .tm .tn .to .tp .tr .travel .tt .tv .tw .tz .ua .ug .uk .us .uy .uz .va .vc .ve .vg .vi .vn .vu .wf .ws .xn--0zwm56d .xn--11b5bs3a9aj6g .xn--80akhbyknj4f .xn--9t4b11yi5a .xn--deba0ad .xn--fiqs8s .xn--fiqz9s .xn--fzc2c9e2c .xn--g6w251d .xn--hgbk6aj7f53bba .xn--hlcj6aya9esc7a .xn--j6w193g .xn--jxalpdlp .xn--kgbechtv .xn--kprw13d .xn--kpry57d .xn--mgbaam7a8h .xn--mgbayh7gpa .xn--mgberp4a5d4ar .xn--o3cw4h .xn--p1ai .xn--pgbs0dh .xn--wgbh1c .xn--xkc2al3hye2a .xn--ygbi2ammx .xn--zckzah .ye .yt .za .zm .zw"), true);
	
	$result = "";
	
	$position = 0;
	while (preg_match($rexUrlLinker, $text, $match, PREG_OFFSET_CAPTURE, $position))
	{
		list($url, $urlPosition) = $match[0];
					
		$result .= htmlspecialchars(substr($text, $position, $urlPosition - $position));
		
		$domain = $match[2][0];
		$port   = $match[3][0];
		$path   = $match[4][0];
					
		$tld = strtolower(strrchr($domain, '.'));
		if (preg_match('{^\.[0-9]{1,3}$}', $tld) || isset($validTlds[$tld]))
		{
			$completeUrl = $match[1][0] ? $url : "http://$url";
				
			$result .= '<a href="' . htmlspecialchars($completeUrl) . '" target="_blank" rel="nofollow">' . htmlspecialchars($completeUrl) . '</a>';
		}
		else
		{		
			$result .= htmlspecialchars($url);
		}
	
		$position = $urlPosition + strlen($url);
	}

	$result .= htmlspecialchars(substr($text, $position));
	return $result;
}

?>